package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.ShopCart;
import com.dingreading.cloud.admin.service.ShopCartService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DoubleUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/system/shopCart")
@Api(tags = "总部-购物车")
public class ShopCartController {

    @Resource
    protected ShopCartService shopCartService;



    @ApiOperation(value = "补充购物车的折扣金额")
    @PostMapping(value = "/updateShopCartDisountPrice", produces = "application/json; charset=UTF-8")
    public R<Object> updateShopCartDisountPrice(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName
    ) {

        List<ShopCart> list = shopCartService.listByDiscountPriceIsNUll();
        if (CollectionUtils.isEmpty(list))
            return R.fail("未查询到数据");

        for (ShopCart cart : list) {
            double discountPrice = DoubleUtil.sub(cart.getOriginalPrice(), cart.getPrice());
            shopCartService.updateDiscountPrice(cart.getId(),discountPrice);
        }
        return R.ok();
    }

}